/******************************************************************************
*
*  MP3Play Widget -- SongEntry
*  Created by Patrick Neeley <patrick@neeley.org>
*             http://www.neeley.org/
*  Created on 2007.08.02
*  Updated on 2007.08.03
*
*  Description:

   An individual song entry that exists in the SongList.  Should only be used 
   by the SongList class when used in the MP3Play program.  Displays and
   provides functionality for a song entry, with play button, etc.

******************************************************************************/


class SongEntry extends Component
{
  public var g_num : Int;
  public var g_file : String;
  public var g_title : String;
  public var g_dlfile : String;
  var g_bAllowDownload : Bool;
  var g_comDuration : Component;
  var g_btnPlay : Button;
  var g_btnDownload : Button;

  public function new (
    root, num:Int, file:String, title:String, duration:String, dlfile:String,
    bAllowDownload:Bool, bVisible:Bool, ?x, ?y, ?w, ?h, ?color, ?text
  ) {
    super(root, true);

    g_bAllowDownload = bAllowDownload;

    this.move(x,y,w,h);
    this.setVisible(bVisible);

    (color == null) ? this.setColor(0xDFDFDF): this.setColor(color);
    this.setAlpha(50);

    (text == null) ? this.setTextColor(0x000000): this.setTextColor(text);

      // check input parameters
    if (file == "") {
      this.destroy();
      return;
    }
    if (title == "") {
      title = file;
    }

      // set the globals
    g_num = num;
    g_file = file;
    g_title = title;
    g_dlfile = dlfile;

      // set the text
    tf.textColor = this.getTextColor();
    var f = tf.getNewTextFormat();
    f.size = 11;
    f.bold = true;
    tf.setNewTextFormat(f);
    tf.text = title;

    var x = w;

    if (duration != "") {
      f.align = "right";
      x -= 42;
      g_comDuration = new Component(this.mc, true);
      g_comDuration.setColor(this.getColor());
      g_comDuration.move(x, 0, 42, h);
      g_comDuration.setTextColor(tf.textColor);
      g_comDuration.setAlpha(0);
      g_comDuration.setVisible(bVisible);
      g_comDuration.setNewTextFormat(f);
      g_comDuration.setText(duration);
    }

    var me = this;

    if (g_bAllowDownload) {
      g_btnDownload = new Button(root, "downloadimg", false);
      g_btnDownload.onPress = this.onDownloadHandler;
      g_btnDownload.onRollOver = function () { me.onRollOverHandler(); }
      g_btnDownload.onRollOut = function () { me.onRollOutHandler(); }
    }

    g_btnPlay = new Button(root, "songplayimg", false);
    g_btnPlay.onPress = this.onPlayHandler;
    g_btnPlay.onRollOver = function () { me.onRollOverHandler(); }
    g_btnPlay.onRollOut = function () { me.onRollOutHandler(); }

      // make sure this class is notified when the MovieClip is clicked on
    this.mc.onRollOver = this.onRollOverHandler;
    this.mc.onRollOut = this.onRollOutHandler;
  }
  // end function new

  public function setVisible (b:Bool) :Bool {
    g_comDuration.setVisible(b);
    g_btnPlay.setVisible(b);
    if (g_bAllowDownload) {
      g_btnDownload.setVisible(b);
    }
    super.setVisible(b);
    return b;
  }
  public function move (x:Float, y:Float, w:Float, h:Float) {
    var bx = w - 40;
    g_comDuration.move(bx, 0, 40, h);
    var bh = 16;
    bx -= bh + 2;
    var by = y + ((h <= bh) ? 0: Math.floor((h/2) - (bh/2)));
    if (g_bAllowDownload) {
      g_btnDownload.move(bx, by, bh, bh);
      bx -= bh + 2;
    }
    g_btnPlay.move(bx, by, bh, bh);
    super.move(x,y,w,h);
  }

  private function onRollOverHandler () {
    this.setAlpha(100);
  }
  private function onRollOutHandler () {
    this.setAlpha(50);
  }

  public function onPlayHandler () {
    this.onPlay(g_num, g_file, g_title, g_dlfile);
  }
  public function onPlay (num:Int, file:String, title:String, dlfile:String) {
    // This is just a placeholder; it must be implemented after an instance
    // of this class has been created.
  }

  public function onDownloadHandler () {
    if (g_bAllowDownload) {
      this.onDownload(g_dlfile);
    }
  }
  public function onDownload (dlfile:String) {
    // This is just a placeholder; it must be implemented after an instance
    // of this class has been created.
  }

  public function destroy ()
  {
    if (g_comDuration != null) {
      g_comDuration.destroy();
    }
    if (g_btnPlay != null) {
      g_btnPlay.destroy();
    }
    if (g_btnDownload != null) {
      g_btnDownload.destroy();
    }
    super.destroy();
  }
  // end function destroy


}
// end class SongEntry
